<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Auth extends Cus_Controller {	
	private $auth;
	
	public function __construct(){
		parent::__construct(); 
		$this->auth = $this->common_auth->check_auth();
	}
	
	public function login()
	{		
		if($this->auth != null)
		{
			$this->common_string->php_redirect(CONS_BASE_URL . 'backend');
		}
		$data['seo']['title'] = 'Đăng nhập vào hệ thống';		
		if($this->input->post('login'))
		{
			$_post = $this->input->post();
			$this->form_validation->set_rules('username', 'Tên đăng nhập', 'trim|required|callback__username');
			$this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required|callback__password['.$_post['username'].']');
			$this->form_validation->set_error_delimiters('<li>', '</li>');
			
			if($this->form_validation->run())
			{
				$user = $this->db->select('username, password, salt')->where(array('username' => $_post['username']))->from('user')->get()->row_array();
				setcookie(CONS_PREFIX. 'user_logged',json_encode($this->common_string->encode_cookie($user['username'])), $this->common_date->get_today(), '/');
				$this->common_string->js_redirect('Đăng nhập hệ thống thành công.', CONS_BASE_URL . 'backend/auth/login');				
			}
		}
		$data['template'] = 'backend/auth/login';		
		$this->load->view('backend/layout/login', isset($data) ? $data : NULL);
	}
	
	public function _username($username)
	{
		$count = $this->db->where(array('username' => $username))->from('user')->count_all_results();
		if($count == 0)
		{
			$this->form_validation->set_message('_username', '%s không tồn tại');
			return FALSE;
		}
		return TRUE;
	}
	
	public function _password($password, $username)
	{
		if($this->_username($username))
		{
			$user = $this->db->select('username, password, salt')->where(array('username' => $username))->from('user')->get()->row_array();
			$password = $this->common_string->encode_password($user['salt'], $password);
			if($user['password'] != $password)
			{
				$this->form_validation->set_message('_password', '%s không hợp lệ');
				return FALSE;
			}			
			return TRUE;
		}
	}
	
	public function forgot()
	{		
		$data['seo']['title'] = 'Quên mật khẩu';
		if($this->input->post('forgot'))
		{
			$this->form_validation->set_rules('email', 'email', 'required|valid_email');
			$this->form_validation->set_error_delimiters('<li>', '</li>');
			if($this->form_validation->run())
			{
				
			}
		}
		$data['template'] = 'backend/auth/forgot';
		$this->load->view('backend/layout/login', isset($data) ? $data : NULL);
	}
	
	public function create()
	{	
		$count = $this->db->from('user')->count_all_results();
		if($count > 0)
		{
			$this->common_string->php_redirect(CONS_BASE_URL . 'backend/auth/login');
		}
		$data['seo']['title'] = 'Tạo tài khoản';
		if($this->input->post('create'))
		{
			$this->form_validation->set_rules('username', 'Tên đăng nhập', 'trim|required');
			$this->form_validation->set_rules('password', 'Mật khẩu', 'trim|required');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			$this->form_validation->set_error_delimiters('<li>', '</li>');
			
			if($this->form_validation->run())
			{
				$_post = $this->input->post();					
				$_post['username'] = isset($_post['username']) ? htmlspecialchars($_post['username']) : "";
				$_post['password'] = $_post['password'] ? htmlspecialchars($_post['password']) : "";
				$_post['email'] = $_post['email'] ? htmlspecialchars($_post['email']) : "";
				$_post = $this->common_string->allow_post($_post, array('username', 'password', 'email'));
				$_post['salt'] = $this->common_string->random(69);
				$_post['password'] = $this->common_string->encode_password($_post['salt'], $_post['password']);
				$_post['created'] = $this->common_date->get_datetime();
				//$this->db->insert('user', $_post);
				$this->load->model('common_model');
				$this->common_model->insert_data('user', $_post);
				$this->common_string->js_redirect('Tạo tài khoản quản trị thành công.', CONS_BASE_URL . 'backend/auth/login');
			}
		}
		$data['template'] = 'backend/auth/create';
		$this->load->view('backend/layout/login', isset($data) ? $data : NULL);
	}
}
